-
Notifications
You must be signed in to change notification settings - Fork 18
fix: Synchronize charge operations to prevent race conditions #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
src/apify/_charging.py
Outdated
| # START OF CRITICAL SECTION - no awaits here | ||
| # Acquire lock to prevent race conditions between concurrent charge calls | ||
| # (e.g., when Actor.push_data with charging is called concurrently with Actor.charge). | ||
| async with self._charge_lock: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This, on its own, doesn't fix the issue of multiple interleaved, PPE-aware Actor.push_data calls. The lock would need to be held for the whole duration of the push_data+charge sequence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, so I moved the lock to the Actor class, take a look.
976e481 to
e34aeed
Compare
janbuchar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| pushed_items_count = min(max_charged_count, len(data)) if max_charged_count is not None else len(data) | ||
| # If charging is requested, acquire the charge lock to prevent race conditions between concurrent | ||
| # push_data calls. We need to hold the lock for the entire push_data + charge sequence. | ||
| async with self._charge_lock: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory, this could still get interleaved with an Actor.charge call with an unrelated event, which would result in pushing more items than the user can afford.
A watertight solution would be to use a read-write lock where plain Actor.charge would acquire the read-lock and Actor.push_data would acquire the write-lock.
But since there is no built-in read-write lock for asyncio, we should probably just leave this the way it is 🙂
Closes: #666